<?php
	defined('RUN') or die('No access');

	class PostController
	{
		public function __construct()
		{
			if (!Auth::check('acp'))
			{
				Url::redirect('admin/auth/login');
			}
		}
		
		public function index()
		{
			$data = Model::get('admin')->getLayoutData();
			$fields='`id`,`name`,`images`';
			$data['posts'] = Model::get('post')->getPost($fields);

			$data['content_file'] = 'post/view.tpl';
			View::load('layout.tpl', $data, 'admin');
		}

		public function add()
		{
			$data = Model::get('admin')->getLayoutData();

			if (Input::post('action') == 'add' && Security::checkToken('post_form', Input::post('token')))
			{
				Upload::setDestDir('uploads' . DS . 'posts');
				Upload::setFileField('images');
				$images = Upload::uploadFile();

				if (Validator::checkRequired(Input::post(), array('txtName')))
				{
					$post = array(
						'name' => strip_tags(Input::post('txtName')),
						'description' => strip_tags(Input::post('description')),
						'keywords' => strip_tags(Input::post('keywords')),
						'content_short' => strip_tags(Input::post('txtShort')),
						'content' => Input::post('content'),
						'images' => ($images == FALSE ? '' : $images),
						'category_id' => 1
					);

					if (Model::get('post')->add($post))
					{
						Session::setFlash('notification', array('type' => 'success', 'msg' => 'Added a new post !'));
						Url::redirect('admin/post');
					}
				}

				$data['failed'] = TRUE;
			}
			
			$data['categories'] = Model::get('category')->getCategoriesAll();
			$data['action'] = 'add';
			$data['content_file'] = 'post/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}

		public function update($post_id)
		{
			$data = Model::get('admin')->getLayoutData();
            $data['post'] = Model::get('post')->getById($post_id);

			if (Input::post('action') == 'update' && Security::checkToken('post_form', Input::post('token')))
			{
				Upload::setDestDir('uploads' . DS . 'posts');
				Upload::setFileField('images');
				$images = Upload::uploadFile();
				if($images=='')
					$images=$data['post']['images'];
				else{
					unlink('.'.$data['post']['images']);	
				}
				
				if (Validator::checkRequired(Input::post(), array('txtName')))
				{
					$post = array(
						'name' => strip_tags(Input::post('txtName')),
						'description' => strip_tags(Input::post('description')),
						'keywords' => strip_tags(Input::post('keywords')),
						'content_short' => strip_tags(Input::post('txtShort')),
						'content' => Input::post('content'),
						'images' => $images,
						'category_id' => 1
					);
					if (Model::get('post')->update($post, $post_id))
					{
						Session::setFlash('notification', array('type' => 'success', 'msg' => 'Updated a new post !'));
						Url::redirect('admin/post');
					}
				}

				$data['failed'] = TRUE;
			}
			
			
			$data['categories'] = Model::get('category')->getCategoriesAll();
			$data['action'] = 'update';
			$data['content_file'] = 'post/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}

		public function delete($post_id, $confirm = 'no')
		{
			$data = Model::get('admin')->getLayoutData();

			if ($confirm == 'yes')
			{
				if (Model::get('post')->delete($post_id))
				{
					Session::setFlash('notification', array('type' => 'success', 'msg' => 'Deleted a post !'));
					Url::redirect('admin/post');
				}
			}

			$data['post'] = Model::get('post')->getById($post_id);
			$data['content_file'] = 'post/delete.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
	}
